//
//  RBSecondKillListCell.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/12.
//

#import "RBSecondKillListCell.h"
#import <Masonry.h>
#import "RanTool.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBSearchGoodsProgressView.h"

@interface RBSecondKillListCell()
//商品图片
@property (nonatomic, strong) UIImageView *goodsImgView;
//
@property (nonatomic, strong) UIView *rightContainerV;
//商品名称
@property (nonatomic, strong) UILabel *goodsNameL;
//抢购
@property (nonatomic, strong) RBSearchGoodsProgressView *progressView;
//剩余
@property (nonatomic, strong) UILabel *stockL;
//价格
@property (nonatomic, strong) UILabel *priceL;
//抢购
@property (nonatomic, strong) UILabel *buyBtn;

@property (nonatomic, strong) RBCellModel *cellModel;

@end

@implementation RBSecondKillListCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = UIColor.whiteColor;
        [self rbSetCornerRadius:Zoom(8)];
    }
    return self;
}

#pragma mark - Setter

- (void)setCellModel:(RBCellModel *)cellModel {
    _cellModel = cellModel;
    //model
    RBSecondKillListModel *model = cellModel.cellData;
    //图片
    [self.goodsImgView setImageWithUrl:model.picurl placeHolder:nil];
    //商品名称
    self.goodsNameL.text = model.good_name;
    //进度条
    CGFloat totalNum = [model.available_sale floatValue];
    CGFloat sellNum = totalNum - [model.exchange_num floatValue];
    CGFloat percent = sellNum / totalNum;
    if (percent < 0.01 && percent != 0) {
        percent = 0.01;
    }
    if (percent > 0.5) {
        self.progressView.titleColor = [UIColor whiteColor];
    } else {
        self.progressView.titleColor = [RBThemeConfig tabBarSelectColor];
    }
    self.progressView.percent = percent;
    //剩余
    self.stockL.text = [NSString stringWithFormat:@"剩余%@", model.exchange_num];
    //售价
    self.priceL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(11)] color:[RBThemeConfig priceRedColor] string:@"¥"];
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(15)] color:[RBThemeConfig priceRedColor] string:model.current_price];
        [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(12)] color:[RBThemeConfig textGrayColor] string:@"\n"];
        [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(12)] color:[RBThemeConfig textGrayColor] string:[NSString stringWithFormat:@"¥%@", model.goods_price] lineSpacing:0 througnLineHeight:1 throughLineColor:[RBThemeConfig textGrayColor]];
    }];
    //
    if (model.isCurrent == YES) {
        self.buyBtn.hidden = NO;
    } else {
        self.buyBtn.hidden = YES;
    }
}

#pragma mark - Getter
//商品图片
- (UIImageView *)goodsImgView {
    if (!_goodsImgView) {
        self.goodsImgView = ({
            UIImageView *view = UIImageView.new;
            view.contentMode = UIViewContentModeScaleAspectFill;
            [view rbSetCornerRadius:Zoom(4)];
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(Zoom(10));
                make.top.mas_equalTo(Zoom(9));
                make.width.height.mas_equalTo(Zoom(115));
            }];
            view;
        });
    }
    return _goodsImgView;
}
//
- (UIView *)rightContainerV {
    if (!_rightContainerV) {
        self.rightContainerV = ({
            UIView *view = UIView.new;
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(Zoom(12));
                make.right.mas_equalTo(Zoom(-13));
                make.bottom.mas_equalTo(-Zoom(12));
                make.left.mas_equalTo(self.goodsImgView.mas_right).offset(Zoom(10));
            }];
            
            view;
        });
        
    }
    return _rightContainerV;
}
//商品名称
- (UILabel *)goodsNameL {
    if (!_goodsNameL) {
        self.goodsNameL = ({
            UILabel *view = [RanTool labelWithFontSize:Zoom(16) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@""];
            [self.rightContainerV addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.mas_equalTo(0);
            }];
            view;
        });
    }
    return _goodsNameL;
}
//进度
- (RBSearchGoodsProgressView *)progressView {
    if (!_progressView) {
        self.progressView = ({
            RBSearchGoodsProgressView *view = RBSearchGoodsProgressView.new;
            view.itemW = Zoom(173);
            view.itemH = Zoom(14);
            [self.rightContainerV addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.top.mas_equalTo(self.goodsNameL.mas_bottom).offset(Zoom(10));
                make.width.mas_equalTo(view.itemW);
                make.height.mas_equalTo(view.itemH);
            }];
            view;
        });
    }
    return _progressView;
}
//剩余
- (UILabel *)stockL {
    if (!_stockL) {
        self.stockL = ({
            UILabel *view = [RanTool labelWithFontSize:Zoom(12) weight:1 color:[RBThemeConfig textGrayColor] align:0 text:@""];
            [self.rightContainerV addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.top.mas_equalTo(self.progressView.mas_bottom).offset(Zoom(9));
            }];
            view;
        });
    }
    return _stockL;
}
//售价
- (UILabel *)priceL {
    if (!_priceL) {
        self.priceL = ({
            UILabel *view = UILabel.new;
            view.numberOfLines = 2;
            [self.rightContainerV addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.top.mas_equalTo(self.stockL.mas_bottom).offset(Zoom(12));
            }];
            view;
        });
    }
    return _priceL;
}
//抢购
- (UILabel *)buyBtn {
    if (!_buyBtn) {
        self.buyBtn = ({
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:0 color:UIColor.whiteColor align:1 text:@"抢购" bgColor:@"#E83922" radius:Zoom(3)];
            [self.rightContainerV addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(-Zoom(5));
                make.width.mas_equalTo(Zoom(47));
                make.height.mas_equalTo(Zoom(24));
            }];
            view;
        });
    }
    return _buyBtn;
}

@end
